package com.example.fbi_text.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.fbi_text.R;
import com.example.fbi_text.bean.User;

import java.util.List;

public class UsersAdapter extends BaseAdapter {
    private List<User> mList;
    private LayoutInflater mLayoutInflater;

    public UsersAdapter(List<User> list, Context context){
        mList = list;
        mLayoutInflater = LayoutInflater.from(context);
    }
    //刷新数据，传入新的数据列表
    public void updateData(List<User> list){
        mList.clear();
        mList.addAll(list);
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public User getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            viewHolder = new ViewHolder();
            convertView = mLayoutInflater.inflate(R.layout.account_item, null);
            viewHolder.usernameText = convertView.findViewById(R.id.username);
            viewHolder.passwordText = convertView.findViewById(R.id.password);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        User user = getItem(position);
        if (user != null){
            convertView.setTag(R.integer.user_tag, user);
            viewHolder.usernameText.setText(user.getUser_name());
            viewHolder.passwordText.setText(user.getUser_password());
        }
        return convertView;
    }

    private static class ViewHolder {
        TextView usernameText;
        TextView passwordText;
    }
}
